import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import login from '../components/login/login.vue'
import main from '../components/main/dashboard.vue'
import panel from '../components/Panel/panel.vue'
import staff from '../components/staff/staff.vue'
import dataview from '../components/report/constrol.vue'
import delivery from '../components/Panel/delivery.vue'
import panelmain from '../components/Panel/main.vue'
import rk from '../components/Panel/instorge.vue'
import ckinfo from '../components/Panel/storeinfo.vue'
import cropinfo from '../components/Panel/cropinfo.vue'
import sc from '../components/Panel/storesearch.vue'
import sale from '../components/sale/main1.vue'
import customer from '../components/sale/customer.vue'
import orders from '../components/sale/orders.vue'
import createorder from '../components/sale/createorder.vue'
import salereport from '../components/report/salereport.vue'
import explore from '../components/dataview/explore.vue'

import policy from '../components/main/main.vue'

import logup from '../components/login/logup.vue'
import manage from '../components/manager/manage.vue'
import formal from '../components/staff/formal_staff.vue'
Vue.use(VueRouter)

const routes = [{
		path: '/',
		name: 'Home',
		component: Home,
		children:[
			{
				path:'',
				component:main,
				meta: {
					requiresAuth: true,
					role:["农场主"]
				},
			},
			{
				path:'panel',
				component:panelmain,
				
				children:[
					{
						path:"",
						component:panel,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					},
					{
						path:"delivery",
						component:delivery,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					},
					{
						path:"rk",
						component:rk,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					},
					{
						path:"storeinfo",
						component:ckinfo,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					},
					{
						path:"cropinfo",
						component:cropinfo,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					},
					{
						path:"search",
						component:sc,
						meta: {
							requiresAuth: true,
							role:["仓库管理员","农场主"]
						},
					}

				],
				meta: {
					requiresAuth: true,
					role:["仓库管理员","农场主"]
				},
				
			},
			{
				path:"staffIM",
				component:staff,
				meta:{
					requiresAuth:true,
					role:["人事","农场主"]
				}
			},
			{
				path:"formal",
				component:formal,
				meta:{
					requiresAuth:true,
					role:["人事","农场主"]
				}
			},
			{
				path:"data",
				component:dataview,
				meta:{
					requiresAuth:true,
					role:["市场调查分析员","农场主"]
				}
			},
			{
				path:"sale",
				component:sale,
				children:[
					{
						path:"",
						component:customer,
						meta:{
							requiresAuth:true,
							role:["销售","农场主"]
						}
					},
					{
						path:"order",
						component:orders,
						meta:{
							requiresAuth:true,
							role:["销售","农场主"]
						}
					},
					{
						path:"add",
						component:createorder,
						meta:{
							requiresAuth:true,
							role:["销售","农场主"]
						}
					}
				],
				meta:{
					requiresAuth:true,
					role:["销售","农场主"]
				}
				
			},
			{
				path:"salereport",
				component:salereport,
				meta:{
					requiresAuth:true,
					role:["市场调查分析员","农场主"]
				}
			},
			{
				path:"explore",
				component:explore,
				meta:{
					requiresAuth:true,
					role:["市场调查分析员","农场主"]
				}
			},
			{
				path:"policy",
				component:policy,
				meta:{
					requiresAuth:true,
					role:["市场调查分析员","农场主"]
				}
			}
		],
		// meta: {
		// 	requiresAuth: true
		// },
	},
	{
		path: '/about',
		name: 'About',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import( /* webpackChunkName: "about" */ '../views/About.vue'),
		meta: {
			requiresAuth: false
		}
	},
	{
		path: '/login',
		name: 'login',
		component: login,
		meta: {
			requiresAuth: false
		}
	},
	{
		path:"/logup",
		name:"logup",
		component:logup,
		meta:{
			requiresAuth:false
		}
	},
	{
		path:"/manage",
		name:"manage",
		component:manage,
		meta:{
			requiresAuth:true,
			role:["系统管理员","农场主"]
		}
	}
]



const router = new VueRouter({
	routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
	if (to.matched.some(r => r.meta.requiresAuth)) {
		if (sessionStorage.getItem('user')){
			var role=JSON.parse(sessionStorage.getItem("user")).role
			var flag=(to.meta.role).indexOf(role)
			if(flag>-1)next()
			
			else {
				alert("权限不足")
				next({path:"/login"})
			}
		} 
		else {
			alert("需要登录哦S")
			next({
				path: "/login"
			})
		}
	} else next()
})

export default router
